package com.sjgtw.cloud.job.controller;

import com.sjgtw.cloud.common.core.exception.job.TaskException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.log.annotation.Log;
import com.sjgtw.cloud.common.log.enums.BusinessType;
import com.sjgtw.cloud.common.security.annotation.PreAuthorize;
import com.sjgtw.cloud.job.common.utils.CronUtils;
import com.sjgtw.cloud.job.domain.SysJob;
import com.sjgtw.cloud.job.service.ISysJobService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * <p>Title: 定时任务信息管理</p>
 * <p>Description: quartz-job执行</p>
 *
 * @author Admin
 * @date 2020-9-21
 */
@RestController
@RequestMapping("/job")
public class SysJobController extends BaseController {
	
    @Autowired
    private ISysJobService jobService;

    /**
     * <p>Title: 查询定时任务列表</p>
     * <p>Description: </p>
     *
     * @param sysJob
     * @return
     */
    @PreAuthorize(hasPermi = "monitor:job:list")
    @GetMapping("/list")
    public TableDataInfo list(SysJob sysJob) {
        startPage();
        List<SysJob> list = jobService.selectJobList(sysJob);
        return getDataTable(list);
    }

    /**
     * <p>Title: 定时任务详细信息</p>
     * <p>Description: </p>
     *
     * @param jobId --KEYID
     * @return
     */
    @PreAuthorize(hasPermi = "monitor:job:query")
    @GetMapping(value = "/{jobId}")
    public AjaxResult getInfo(@PathVariable("jobId") Long jobId) {
        return AjaxResult.success(jobService.selectJobById(jobId));
    }

    /**
     * <p>Title: 新增定时任务</p>
     * <p>Description: </p>
     *
     * @param sysJob
     * @return
     * @throws SchedulerException
     * @throws TaskException
     */
    @PreAuthorize(hasPermi = "monitor:job:add")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysJob sysJob) throws SchedulerException, TaskException {
    	
    	if (!CronUtils.isValid(sysJob.getCronExpression()))
        {
            return error("新增任务'" + sysJob.getJobName() + "'失败，Cron表达式不正确");
        }
    	sysJob.setCreateBy(SecurityUtils.getUsername());
    	return toAjax(jobService.insertJob(sysJob));
    }

    /**
     * <p>Title: 修改定时任务</p>
     * <p>Description: </p>
     *
     * @param sysJob
     * @return
     * @throws SchedulerException
     * @throws TaskException
     */
    @PreAuthorize(hasPermi = "monitor:job:edit")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysJob sysJob) throws SchedulerException, TaskException {
    	
    	if (!CronUtils.isValid(sysJob.getCronExpression()))
        {
            return error("修改任务'" + sysJob.getJobName() + "'失败，Cron表达式不正确");
        }
    	sysJob.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(jobService.updateJob(sysJob));
    }

    /**
     * <p>Title: 修改定时任务状态</p>
     * <p>Description: 0正常 1暂停</p>
     *
     * @param job
     * @return
     * @throws SchedulerException
     */
    @PreAuthorize(hasPermi = "monitor:job:changeStatus")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysJob sysJob) throws SchedulerException {
        SysJob newJob = jobService.selectJobById(sysJob.getJobId());
        newJob.setStatus(sysJob.getStatus());
        return toAjax(jobService.changeStatus(newJob));
    }

    /**
     * <p>Title: 任务执行</p>
     * <p>Description: 立即执行一次定时任务</p>
     *
     * @param job
     * @return
     * @throws SchedulerException
     */
    @PreAuthorize(hasPermi = "monitor:job:changeStatus")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/run")
    public AjaxResult run(@RequestBody SysJob sysJob) throws SchedulerException {
    	
    	boolean result = jobService.run(sysJob);
        return result ? success() : error("执行失败，任务不存在或已过期！");
    }

    /**
     * <p>Title: 删除定时任务</p>
     * <p>Description: </p>
     *
     * @param jobIds
     * @return
     * @throws SchedulerException
     * @throws TaskException
     */
    @PreAuthorize(hasPermi = "monitor:job:remove")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public AjaxResult remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException {
        jobService.deleteJobByIds(jobIds);
        return AjaxResult.success();
    }
}
